package com.jie.mc.event;

import com.jie.mc.gui.renqiInventory;
import com.jie.mc.util.VaultUtil;
import org.bukkit.Material;
import org.bukkit.entity.HumanEntity;
import org.bukkit.entity.Player;
import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener;
import org.bukkit.event.inventory.InventoryClickEvent;
import org.bukkit.inventory.Inventory;

public class InventoryGuiEvent implements Listener {
    @EventHandler
    public void InventoryrenqiGui(InventoryClickEvent event){
        Inventory inventory =event.getInventory();
        String title = inventory.getTitle();
        if(title.equalsIgnoreCase(renqiInventory.PlayerrenqiGui))
        {
            HumanEntity whoClicked = event.getWhoClicked();
            Player player=(Player) whoClicked;
            int rawSlot = event.getRawSlot();
            if(event.getRawSlot()==49){
                // todo 进行收购
                event.setCancelled(true);
                player.sendMessage("收购成功");


                int count = 0;
                for (int i = 1; i <5 ; i++) {
                    for (int j = 1; j <8 ; j++) {
                        if (inventory.getItem(9*i+j)!=null){
                            if(inventory.getItem(9*i+j).getType()== Material.EMERALD)

                            {   int num=inventory.getItem(9*i+j).getAmount();
                                count=count+num;
                                player.sendMessage((9*i+j)+"号格子含有绿宝石"+num+"个");
                                inventory.clear(9*i+j);}
                        }

                    }
                }

                player.sendMessage("当前的金钱数量为"+VaultUtil.seemoney(player.getUniqueId()));
                player.sendMessage("总共收购绿宝石的数量为"+count+"个,单价为100,总共获得了"+100*count+"的收入");
                VaultUtil.give(player.getUniqueId(),100*count);
                player.sendMessage("当前的金钱数量为"+VaultUtil.seemoney(player.getUniqueId()));
            }
            else if (rawSlot>=0&&rawSlot<=8||rawSlot<=53&&rawSlot>=45||rawSlot==9||rawSlot==18||rawSlot==27||rawSlot==36||rawSlot==26||rawSlot==35||rawSlot==44){
                event.setCancelled(true);
                player.sendMessage("这个不能点击");
            }

        }




    }
}
